﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;
using System.Text;
using NHibernate;
using Thaire.Investment.BL.DTO;
using Thaire.Investment.BL.Filter;
using Thaire.Investment.BL.ScreenData;
using Thaire.Investment.Domains.Master;
using Thaire.Investment.Domains.Transaction;
using Thaire.Investment.Repository;

namespace Thaire.Investment.BL
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class InvestmentService : IInvestmentService
    {

        #region common

        public DateTime GetSystemDateTime()
        {
            return SessionManager.SystemDate;
        }

        public IList<Domains.Master.CounterParties> GetAllCounterPaties()
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetAllCounterPaties();
        }

        public IList<Domains.Master.PaymentType> GetAllPaymentTypes()
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetAllPaymentTypes();
        }

        public IList<Domains.Master.Bank> GetBranchByBankID(string bankCode)
        {
            return new BankRepo(SessionManager.CurrentSession).GetBranch(bankCode);
        }
                    
        public List<FundName> GetFundThNamesByName(string fundThName)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFundThNamesByName(fundThName);
        }
        public List<FundName> GetFundEnNamesByName(string fundEnName)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFundEnNamesByName(fundEnName);
        }
        public IList<string> GetEquityFullNameTH()
        {
            var datas = new EquityHeaderRepo(SessionManager.CurrentSession).GetFullNameTH();
            var list = new List<string>();
            foreach (var data in datas)
            {
                list.Add(data.FullNameTH);
            }
            return list;
        }

        public IList<string> GetEquityFullNameEN()
        {
            var datas = new EquityHeaderRepo(SessionManager.CurrentSession).GetFullNameEN();
            var list = new List<string>();
            foreach (var data in datas)
            {
                list.Add(data.FullNameEN);
            }
            return list;
        }


        public int? GetRiskLevel(RatingHistory rateHistory)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstrumentBL(session).GetRiskLevel(rateHistory);
        }

        #endregion

        #region Instrument --> Fixed Income Securities


        public List<RatingHistoryViewDTO> GetRatingHistory(int instrumentID, String instrumentType)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstrumentBL(session).GetRatingHistory(instrumentID, instrumentType);
        }

        public FixIncomePurchaseScreenData GetFixIncomePurchaseScreenData()
        {
            ISession session = SessionManager.CurrentSession;
            return new InstrumentBL(session).GetFixIncomePurchaseScreenData();
        }

        public AmortizeScreenData GenerateAmortize(int fixIncomePurchaseID)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstrumentBL(session).GenerateAmortize(fixIncomePurchaseID);
        }

        public FixIncomeSecuritiesDTO InsertFixIncomeHeader(FixIncomeSecuritiesDTO data)
        {


            ISession session = SessionManager.CurrentSession;
            FixIncomeSecuritiesDTO result = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {

                    result = new InstrumentBL(session).InsertFixIncomeHeader(data);

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }

            }
            return result;


        }

        public FixIncomeSecuritiesDTO UpdateFixIncomeHeader(FixIncomeSecuritiesDTO data)
        {

            ISession session = SessionManager.CurrentSession;
            FixIncomeSecuritiesDTO result = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {

                    result = new InstrumentBL(session).UpdateFixIncomeHeader(data);

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }

            }
            return result;

        }

        public List<FixIncomeSecuritiesHeader> SearchFiexdIncome(SearchFiexdIncomeFilter searchData)
        {
            //var dt = SystemUtil.GetSysDate(SessionManager.CurrentSession);

            return new InstrumentBL(SessionManager.CurrentSession).SearchFiexdIncome(searchData);
        }

        public FixIncomeSecuritiesDTO GetFixIncomeHeaderByID(int id)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixIncomeHeaderByID(id);
        }

        public FixedIncomeListData GetFixedIncomeListScreenData()
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixedIncomeListScreenData();
        }

        public List<FixincomesecuritiesPurchase> GetFixIncomePurchasesByHeaderId(int headerId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixIncomePurchasesByHeaderId(headerId);
        }

        public FixIncomeSecuritiesHeader GetFixedIncomeSecuritiesHeaderById(int headerId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixedIncomeSecuritiesHeaderById(headerId);
        }

        public FixincomesecuritiesPurchase InsertFixedIncomePurchase(FixincomesecuritiesPurchase fixincomesecuritiesPurchase)
        {
            ISession session = SessionManager.CurrentSession;
            FixincomesecuritiesPurchase data = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    data = new InstrumentBL(session).InsertFixedIncomePurchase(fixincomesecuritiesPurchase);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }
            return data;
        }

        public FixedIncomeHeaderScreenData GetFixedIncomeHeaderScreenData()
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixedIncomeHeaderScreenData();
        }

        public FixincomesecuritiesPurchaseDTO GetFixIncomePurchaseByID(int purchaseID)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixIncomePurchaseByID(purchaseID);
        }
        public FixIncomeSecuritiesTransactionScreenData GetFixIncomeSecuritiesTransactionScreenData(int purchaseID, int? transactionID)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixIncomeSecuritiesTransactionScreenData(purchaseID, transactionID);
        }


        public FixIncomeSecuritiesTransaction InsertFixIncomeSecuritiesTransaction(FixIncomeSecuritiesTransaction transaction)
        {
            ISession session = SessionManager.CurrentSession;
            FixIncomeSecuritiesTransaction result = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    result = new InstrumentBL(session).InsertFixIncomeSecuritiesTransaction(transaction);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }

            }
            return result;
        }

        public FixIncomeSecuritiesTransactionDTO GetFixIncomeSecuritiesTransactionDTO(int transactionID)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetFixIncomeSecuritiesTransactionDTO(transactionID);
        }

        public FixIncomeSecuritiesTransaction UpdateFixIncomeSecuritiesTransaction(FixIncomeSecuritiesTransaction transaction)
        {
            ISession session = SessionManager.CurrentSession;
            FixIncomeSecuritiesTransaction result = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {

                    result = new InstrumentBL(session).UpdateFixIncomeSecuritiesTransaction(transaction);

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }

            }
            return result;
        }
        public FixincomesecuritiesPurchase UpdateFixincomesecuritiesPurchase(FixincomesecuritiesPurchase purchase)
        {
            ISession session = SessionManager.CurrentSession;
            FixincomesecuritiesPurchase result = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    result = new InstrumentBL(session).UpdateFixincomesecuritiesPurchase(purchase);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }



        public FixincomesecuritiesAmortizeSchedule InsertAmortizeSchedule(FixincomesecuritiesAmortizeSchedule data)
        {
            ISession session = SessionManager.CurrentSession;
            FixincomesecuritiesAmortizeSchedule result = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    result = new InstrumentBL(session).InsertAmortizeSchedule(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }

        public void DeleteAmortizeSchedule(int amortizeScheduleID)
        {
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    new InstrumentBL(session).DeleteAmortizeSchedule(amortizeScheduleID);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }

        }

        public AmortizeScheduleScreenData GetAmortizeScheduleScreenData(int headerID)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstrumentBL(session).GetAmortizeScheduleScreenData(headerID);
        }

        #endregion

        #region Instrument -->Unit Trust
        public UnitTrustProportion InsertUnitTrustProportion(UnitTrustProportion data)
        {
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    data = new InstrumentBL(session).InsertUnitTrustPrivilege(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return data;
        }
        public UnitTrustProportion UpdateUnitTrustProportion(UnitTrustProportion data)
        {
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    data = new InstrumentBL(session).UpdateUnitTrustProportion(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return data;
        }
        public UnitTrustProportionScreenData GetUnitTrustProportionScreenData(int? headerId, int? proportionId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetUnitTrustProportionScreenData(headerId, proportionId);
        }

        public List<UnitTrustHeader> SearchUnitTrust(SearchUnitTrustFilter searchData)
        {
            return new InstrumentBL(SessionManager.CurrentSession).SearchUnitTrust(searchData);
        }

        public UnitTrustTransactionScreenData GetUnitTrustTransactionScreenData(int? headerId, int? transactionId, string transactionType)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetUnitTrustTransactionScreenData(headerId, transactionId, transactionType);
        }

        public UnitTrustHeaderScreenData GetUnitTrustHeaderScreenData(int? headerId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetUnitTrustHeaderScreenData(headerId);
        }

        public UnitTrustHeaderDTO InsertUnitTrustHeader(UnitTrustHeader data)
        {
            UnitTrustHeaderDTO result = null;
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    result = new InstrumentBL(session).InsertUnitTrustHeader(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }

        public UnitTrustHeaderDTO UpdateUnitTrustHeader(UnitTrustHeader data)
        {
            UnitTrustHeaderDTO result = null;
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    result = new InstrumentBL(session).UpdateUnitTrustHeader(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }

        public UnitTrustTransaction InsertUnitTrustTransaction(UnitTrustTransaction data)
        {
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    data = new InstrumentBL(session).InsertUnitTrustTransaction(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return data;
        }

        public UnitTrustTransaction UpdateUnitTrustTransaction(UnitTrustTransaction data)
        {
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    data = new InstrumentBL(session).UpdateUnitTrustTransaction(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return data;
        }

        public UnitTrustPrivilegeScreenData GetUnitTrustPrivilegeScreenData(int? headerId, int? privilegeId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetUnitTrustPrivilegeScreenData(headerId, privilegeId);
        }

        public UnitTrustHeaderDTO GetPrivilegeDataByHeaderId(int headerId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetPrivilegeDataByHeaderId(headerId);
        }

        public UnitTrustPrivilege UpdateUnitTrustPrivilege(UnitTrustPrivilege data)
        {
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    data = new InstrumentBL(session).UpdateUnitTrustPrivilege(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return data;
        }

        public UnitTrustPrivilege InsertUnitTrustPrivilege(UnitTrustPrivilege data)
        {
            var session = SessionManager.CurrentSession;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    data = new InstrumentBL(session).InsertUnitTrustPrivilege(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return data;
        }

        #endregion


        #region Instrument -->Equity

        public List<EquityHeader> SearchEquity(SearchEquityFilter searchData)
        {
            return new EquityBL(SessionManager.CurrentSession).SearchEquity(searchData);
        }

        public EquityHeaderScreenData GetEquityHeaderScreenData(int? headerId)
        {
            return new EquityBL(SessionManager.CurrentSession).GetEquityHeaderScreenData(headerId);
        }

        public EquityHeaderDTO InsertEquityHeader(EquityHeader data)
        {
            ISession session = SessionManager.CurrentSession;
            EquityHeaderDTO dto = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    dto = new EquityBL(SessionManager.CurrentSession).InsertEquityHeader(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return dto;
        }

        public EquityHeaderDTO UpdateEquityHeader(EquityHeader data)
        {
            ISession session = SessionManager.CurrentSession;
            EquityHeaderDTO dto = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    dto = new EquityBL(SessionManager.CurrentSession).UpdateEquityHeader(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return dto;
        }

        public EquityTransaction InsertEquityTransaction(EquityTransaction data)
        {
            ISession session = SessionManager.CurrentSession;
            EquityTransaction item = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    item = new EquityBL(SessionManager.CurrentSession).InsertEquityTransaction(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return item;
        }

        public EquityTransaction UpdateEquityTransaction(EquityTransaction data)
        {
            ISession session = SessionManager.CurrentSession;
            EquityTransaction item = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    item = new EquityBL(SessionManager.CurrentSession).UpdateEquityTransaction(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return item;
        }

        public EquityTransactionScreenData GetEquityTransactionScreenData(int? headerId, int? transactionId, string transactionType)
        {
            return new EquityBL(SessionManager.CurrentSession).GetEquityTransactionScreenData(headerId, transactionId, transactionType);
        }

        public EquityHeaderDTO GetEquityPrivilegeDataByHeaderId(int headerId)
        {
            return new EquityBL(SessionManager.CurrentSession).GetEquityPrivilegeDataByHeaderId(headerId);
        }

        public EquityPrivilege InsertEquityPrivilege(EquityPrivilege data)
        {
            ISession session = SessionManager.CurrentSession;
            EquityPrivilege item = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    item = new EquityBL(SessionManager.CurrentSession).InsertEquityPrivilege(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return item;
        }

        public EquityPrivilege UpdateEquityPrivilege(EquityPrivilege data)
        {
            ISession session = SessionManager.CurrentSession;
            EquityPrivilege item = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    item = new EquityBL(SessionManager.CurrentSession).UpdateEquityPrivilege(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return item;
        }

        public EquityPrivilegeScreenData GetEquityPrivilegeScreenData(int? headerId, int? privilegeId)
        {
            return new EquityBL(SessionManager.CurrentSession).GetEquityPrivilegeScreenData(headerId, privilegeId);
        }

        #endregion

        #region Instrucation



        public ManageInstructionScreenData GetManageInstructionScreenData()
        {
            ISession session = SessionManager.CurrentSession;
            return new InstructionBL(session).GetManageInstructionScreenData();
        }

        public List<ManageInstructionDTO> SearchInstructionDTO(SearchInstructionFilter filter)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstructionBL(session).SearchInstructionDTO(filter);

        }


        public InstructionFormScreenData GetInstructionFormScreenData(int headerID)
        {
            ISession session = SessionManager.CurrentSession;

            return new InstructionBL(session).GetInstructionFormScreenData(headerID);

        }


        public List<InstructionDataDTO> InstructionGetSelectionFixincomeData(InstructionFormScreenData formData)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstructionBL(session).InstructionGetSelectionFixIncomeData(formData);
        }

        public List<InstructionEquityGridDetail> InstructionGetSelectionEquity(InstructionFormScreenData formData)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstructionBL(session).InstructionGetSelectionEquity(formData);
        }

        public List<InstructionUnitTrustGridDetail> InstructionGetSelectionUnitTrustData(InstructionFormScreenData formData)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstructionBL(session).InstructionGetSelectionUnitTrustData(formData);
        }
        public void DeleteInstructionFixIncome(int[] delID)
        {
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    InstructionDetailRepo repo = new InstructionDetailRepo(session);
                    foreach (var item in delID)
                    {
                        if (item != 0)
                        {
                            repo.DeleteByID(item);
                        }
                    }

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }

        }

        public void DeleteInstructionDeposit(int[] delID)
        {
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    DepositInstructionDetailRepo repo = new DepositInstructionDetailRepo(session);
                    foreach (var item in delID)
                    {
                        if (item != 0)
                        {
                            repo.DeleteByID(item);
                        }
                    }

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }

        }


        public void DeleteInstructionUnitTrust(int[] delID)
        {

            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    UnitTrustInstructionDetailRepo repo = new UnitTrustInstructionDetailRepo(session);
                    foreach (var item in delID)
                    {
                        if (item != 0)
                        {
                            repo.DeleteByID(item);
                        }
                    }

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }

        public void DeleteInstructionEquity(int[] delID)
        {

            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    EquityInstructionDetailRepo repo = new EquityInstructionDetailRepo(session);
                    foreach (var item in delID)
                    {
                        if (item != 0)
                        {
                            repo.DeleteByID(item);
                        }
                    }

                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }

        public InstructionFormScreenData SaveInstructionFixIncome(InstructionHeader header, List<InstructionDetail> detail)
        {

            InstructionFormScreenData result = null;
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                int headerID = 0;
                InstructionBL bl = new InstructionBL(session);
                try
                {

                    headerID = bl.SaveInstruction(header, detail);
                    tran.Commit();

                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
                result = bl.GetInstructionFormScreenData(headerID);
            }

            return result;
        }
        public InstructionFormScreenData SaveInstructionUnitTrust(InstructionHeader header, List<UnitTrustInstructionDetail> detail)
        {

            InstructionFormScreenData result = null;
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                int headerID = 0;
                InstructionBL bl = new InstructionBL(session);
                try
                {

                    headerID = bl.SaveInstructionUnitTrust(header, detail);
                    tran.Commit();

                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
                result = bl.GetInstructionFormScreenData(headerID);
            }

            return result;
        }


        public InstructionFormScreenData SaveInstructionEquity(InstructionHeader header, List<EquityInstructionDetail> detail)
        {

            InstructionFormScreenData result = null;
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                int headerID = 0;
                InstructionBL bl = new InstructionBL(session);
                try
                {

                    headerID = bl.SaveInstructionEquity(header, detail);
                    tran.Commit();

                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
                result = bl.GetInstructionFormScreenData(headerID);
            }

            return result;
        }

        public List<InstructionDepositAtFinancialGridDetail> InstructionGetSelectionDepositData(InstructionFormScreenData formData)
        {
            ISession session = SessionManager.CurrentSession;
            return new InstructionBL(session).InstructionGetSelectionDepositData(formData);
        }

        public InstructionFormScreenData SaveInstructionDeposit(InstructionHeader header, List<DepositInstructionDetail> detail)
        {
            InstructionFormScreenData result = null;
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                int headerID = 0;
                InstructionBL bl = new InstructionBL(session);
                try
                {

                    headerID = bl.SaveInstructionDeposit(header, detail);
                    tran.Commit();

                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
                result = bl.GetInstructionFormScreenData(headerID);
            }

            return result;
        }
        #endregion

        #region Instrument --> Deposit

        public List<DepositHeaderDTO> SearchDeposit(SearchDepositFilter searchData)
        {
            return new DepositBL(SessionManager.CurrentSession).SearchDeposit(searchData);
        }

        public DepositHeaderScreenData GetDepositHeaderScreenData(int? headerId)
        {
            return new DepositBL(SessionManager.CurrentSession).GetDepositHeaderScreenData(headerId);
        }

        public DepositHeaderDTO InsertDepositHeader(DepositHeaderDTO headerDTO)
        {
            ISession session = SessionManager.CurrentSession;
            DepositHeaderDTO dto = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    dto = new DepositBL(SessionManager.CurrentSession).InsertDepositHeader(headerDTO);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return dto;
        }

        public DepositInterest InsertDepositInterest(DepositInterest data)
        {
            ISession session = SessionManager.CurrentSession;
            DepositInterest item = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    item = new DepositBL(SessionManager.CurrentSession).InsertDepositInterest(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return item;
        }

        public void DeleteDepositInterest(int interestID)
        {
            ISession session = SessionManager.CurrentSession;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    new DepositBL(SessionManager.CurrentSession).DeleteDepositInterest(interestID);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }

        }

        public DepositHeaderDTO UpdateDepositHeader(DepositHeaderDTO headerDTO)
        {
            ISession session = SessionManager.CurrentSession;
            DepositHeaderDTO dto = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    dto = new DepositBL(SessionManager.CurrentSession).UpdateDepositHeader(headerDTO);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return dto;
        }

        public DepositTransaction InsertDepositTransaction(DepositTransaction data)
        {
            ISession session = SessionManager.CurrentSession;
            DepositTransaction item = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    item = new DepositBL(SessionManager.CurrentSession).InsertDepositTransaction(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return item;
        }

        public DepositTransaction UpdateDepositTransaction(DepositTransaction data)
        {
            ISession session = SessionManager.CurrentSession;
            DepositTransaction item = null;
            using (ITransaction tran = session.BeginTransaction())
            {
                try
                {
                    item = new DepositBL(SessionManager.CurrentSession).UpdateDepositTransaction(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return item;
        }

        public DepositTransactionScreenData GetDepositTransactionScreenData(int? headerId, int? transactionId, string transactionType)
        {
            return new DepositBL(SessionManager.CurrentSession).GetDepositTransactionScreenData(headerId, transactionId, transactionType);
        }

        public DepositInterestScreenData GetDepositInterestScreenData(int headerId)
        {
            return new DepositBL(SessionManager.CurrentSession).GetDepositInterestScreenData(headerId);
        }

        #endregion


        #region Instrument --> Loan

        public LoanExtraInstallmentScreenData GetLoanExtraInstallmentScreenData(int? headerId, int? extraInstallmentId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetLoanExtraInstallmentScreenData(headerId, extraInstallmentId);
        }

        public LoansExtrainstallment UpdateLoansExtrainstallment(LoansExtrainstallment data)
        {
            return new InstrumentBL(SessionManager.CurrentSession).UpdateLoansExtrainstallment(data);
        }
        public LoansExtrainstallment InsertLoansExtrainstallment(LoansExtrainstallment data)
        {
            return new InstrumentBL(SessionManager.CurrentSession).InsertLoansExtrainstallment(data);
        }

        public List<LoansHeader> SearchLoans(SearchLoanFilter filter)
        {
            return new InstrumentBL(SessionManager.CurrentSession).SearchLoans(filter);
        }

        public LoanHeaderScreenData GetLoanHeaderScreenData(int? headerId, bool isSearchMode)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetLoanHeaderScreenData(headerId, isSearchMode);
        }

        public LoanHeaderDTO UpdateLoanHeader(LoansHeader data)
        {
            var session = SessionManager.CurrentSession;
            LoanHeaderDTO dto = null;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    dto = new InstrumentBL(SessionManager.CurrentSession).UpdateLoanHeader(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return dto;
        }

        public LoanHeaderDTO InsertLoanHeader(LoansHeader data)
        {
            var session = SessionManager.CurrentSession;
            LoanHeaderDTO dto = null;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    dto = new InstrumentBL(SessionManager.CurrentSession).InsertLoanHeader(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return dto;
        }

        public LoanInterestRateScreenData GetLoanInterestRateScreenData(int? headerId)
        {
            return new InstrumentBL(SessionManager.CurrentSession).GetLoanInterestRateScreenData(headerId);
        }

        public LoansInterestDetail InsertInterestRateDetail(LoansInterestDetail detail)
        {
            var session = SessionManager.CurrentSession;
             LoansInterestDetail result = null;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                   result =  new InstrumentBL(SessionManager.CurrentSession).InsertInterestRateDetail(detail);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }

        public LoansInterestDetail UpdateInterestRateDetail(LoansInterestDetail data)
        {
            var session = SessionManager.CurrentSession;
            LoansInterestDetail result = null;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    result = new InstrumentBL(SessionManager.CurrentSession).UpdateInterestRateDetail(data);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }

        public AmortizeLoanScheduleScreenData GetAmortizeLoanScheduleScreenData(int interestDetailID)
        {
            var session = SessionManager.CurrentSession;
            AmortizeLoanScheduleScreenData result = null;
            using (var tran = session.BeginTransaction())
            {
                try
                {
                    result = new InstrumentBL(SessionManager.CurrentSession).GetAmortizeLoanScheduleScreenData(interestDetailID);
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }
        #endregion



    }
}
